/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.interceptor;

import com.jspx.boot.environment.Environment;
import com.jspx.boot.sign.UserType;
import com.jspx.boot.sign.YesNoType;
import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.ActionInvocation;
import com.jspx.txweb.IUserSession;
import com.jspx.txweb.bundle.Bundle;
import com.jspx.txweb.dao.PermissionDAO;
import com.jspx.txweb.env.TXWebIoc;
import com.jspx.txweb.online.OnlineManager;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.table.MemberRole;
import com.jspx.txweb.table.Role;
import com.jspx.utils.ArrayUtil;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: chenyuan
 * Date: 12-7-28
 * Time: 下午11:54
 * 权限拦截 ,规则为,只判断游客是否允许进入,并且放入用户的信息和角色信息
 * 是PermissionInterceptor  的简化版本
 *
 */
public class UserInterceptor extends InterceptorSupport {

    public void init()
    {

    }

    public void destroy() {

    }

    //允许游客登录
    private boolean guestLogin = false;
    public boolean isGuestLogin() {
        return guestLogin;
    }

    public void setGuestLogin(boolean guestLogin) {
        this.guestLogin = guestLogin;
    }

    private String guestResult = ActionSupport.LOGIN;
    public void setGuestResult(String guestResult) {
        this.guestResult = guestResult;
    }

    /**
     * 登陆页面入口必须排除
     */
    private String[] ruleOutUrl;
    public void setRuleOutUrl(String[] ruleOutUrl) {
        this.ruleOutUrl = ruleOutUrl;
    }


    private boolean permission = true;
    public void setPermission(boolean permission) {
        this.permission = permission;
    }

    /**
     * 载入在线管理
     */
    private OnlineManager onlineManager;
    @Ref(name = Environment.onlineManager)
    public void setOnlineManager(OnlineManager onlineManager) {
        this.onlineManager = onlineManager;
    }

    private PermissionDAO permissionDAO;
    @Ref(name = TXWebIoc.permissionDAO)
    public void setPermissionDAO(PermissionDAO permissionDAO) {
        this.permissionDAO = permissionDAO;
    }



    public String intercept(ActionInvocation actionInvocation) throws Exception {
        ActionSupport action = actionInvocation.getActionProxy().getAction();
        IUserSession userSession = onlineManager.getUserSession(action);
        action.put(ActionSupport.Key_UserSession,userSession);
        String namespace = action.getEnv(ActionSupport.Key_Namespace);
        String actionName = actionInvocation.getActionName();
        //登陆入口，直接放行
        if (ArrayUtil.inArray(ruleOutUrl, "/" + namespace + "/" + actionName, true)||ArrayUtil.inArray(new String[]{ActionSupport.ERROR,ActionSupport.UNTITLED,ActionSupport.LOGIN},actionName,true))
        {
            return actionInvocation.invoke();
        }
        //自动分配测试权限 begin
        if (!permission&& (Environment.SYSTEM_ID==userSession.getUid()||userSession.getUid()==1))
        {
            Role role = createDebugRole();
            role.setNamespace(permissionDAO.getNamespace());
            role.setIp(userSession.getIp());
            userSession.setRole(role);
        }

        if (action.isGuest())
        {
            userSession.setRole(permissionDAO.getRole(config.getInt(Environment.guestRole)));
        } else
        if (userSession.getRole(permissionDAO.getNamespace()).getId()==0)
        {
            List<MemberRole> roles = permissionDAO.getMemberRoles(userSession.getUid());
            for (MemberRole memberRole:roles)
            {
                userSession.setRole(memberRole.getRole());
            }
        }
        if (userSession.getRole(permissionDAO.getNamespace()).getId()==0)
        {
            userSession.setRole(permissionDAO.getRole(config.getInt(Environment.registerRole)));
        }

        action.put(ActionSupport.Key_UserSession, userSession);
        if (guestLogin&&userSession.isGuest()&&!userSession.getRole(permissionDAO.getNamespace()).checkOperate(namespace,actionName,actionInvocation.getActionProxy().getMethod()))
        {
            action.addFieldInfo("提示", "没有登录");
            return guestResult;
        }

        return actionInvocation.invoke();
        //也可以 return Action.ERROR; 终止action的运行
    }
}